package com.stm.bi.model;

import com.alibaba.fastjson.JSONObject;
import com.stm.framework.annotation.Param;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 简介
 *
 * @author zhouyuanpeng
 * @date 2024-05-23 11:58
 */
public class ReturnRateData extends BaseOriginData {
    @Param(label = "退货率")
    private BigDecimal returnRate;
    @Param(label = "销量")
    private BigDecimal qty;

    @Param(label = "总销量")
    private BigDecimal totalQty;

    @Param(label = "退货量")
    private BigDecimal returnQty;

    @Param(label = "累计销量")
    private BigDecimal cumulativeQty ;

    @Param(label = "累计总销量")
    private BigDecimal cumulativeTotalQty;

    @Param(label = "累计退货量")
    private BigDecimal  cumulativeReturnQty;

    @Override
    public void initData() {
        if (this.totalQty != null && this.returnQty != null) {
            // 退货率 = 退货量 / 总销量
            if (this.totalQty.compareTo(BigDecimal.ZERO) != 0 ) {
                this.returnRate = this.returnQty.divide(this.totalQty, 6, RoundingMode.HALF_UP);
            } else {
                // 默认值
                this.returnRate = BigDecimal.ZERO;
            }
        }
        this.fieldValue1 = this.returnRate;
        super.initData();
    }

    @Override
    public void cumulativeData(JSONObject json, String totalType) {
        if (json != null) {
            ReturnRateData obj = JSONObject.toJavaObject(json, ReturnRateData.class);
            if (obj.getQty() != null) {
                this.cumulativeQty = obj.getQty();
            }
            if (obj.getTotalQty() != null) {
                this.cumulativeTotalQty = obj.getTotalQty();
            }
            if (obj.getReturnQty() != null) {
                this.cumulativeReturnQty = obj.getReturnQty();
            }

            // 累计退货率 = 累计退货量 / 累计总销量
            if (this.cumulativeTotalQty != null  && this.cumulativeTotalQty.compareTo(BigDecimal.ZERO) != 0 ) {
                this.returnRate = this.cumulativeReturnQty.divide(this.cumulativeTotalQty, 6, RoundingMode.HALF_UP);
            } else {
                // 默认值
                this.returnRate = BigDecimal.ZERO;
            }
            this.fieldValue1 = this.returnRate;
            super.initData();
        }
    }


    @Override
    public void totalQty(JSONObject json, String totalType) {
        ReturnRateData obj = JSONObject.toJavaObject(json, ReturnRateData.class);
        // 累计销量
        if (obj.getQty() != null) {
            this.qty =  this.qty == null ? obj.getQty() : this.qty.add(obj.getQty());
        }
        if (obj.getTotalQty() != null) {
            this.totalQty = this.totalQty == null ? obj.getTotalQty() : this.totalQty.add(obj.getTotalQty());
        }
        if (obj.getReturnQty() != null) {
            this.returnQty = this.returnQty == null ? obj.getReturnQty() : this.returnQty.add(obj.getReturnQty());
        }

        // 累计退货率 = 累计退货量 / 累计总销量
        if (this.totalQty != null  && this.totalQty.compareTo(BigDecimal.ZERO) != 0 ) {
            this.returnRate = this.returnQty.divide(this.totalQty, 6, RoundingMode.HALF_UP);
        } else {
            // 默认值
            this.returnRate = BigDecimal.ZERO;
        }
        this.fieldValue1 = this.returnRate;
        super.initData();
    }

    public BigDecimal getReturnRate() {
        return returnRate;
    }

    public ReturnRateData setReturnRate(BigDecimal returnRate) {
        this.returnRate = returnRate;
        return this;
    }

    public BigDecimal getQty() {
        return qty;
    }

    public ReturnRateData setQty(BigDecimal qty) {
        this.qty = qty;
        return this;
    }

    public BigDecimal getTotalQty() {
        return totalQty;
    }

    public ReturnRateData setTotalQty(BigDecimal totalQty) {
        this.totalQty = totalQty;
        return this;
    }

    public BigDecimal getReturnQty() {
        return returnQty;
    }

    public ReturnRateData setReturnQty(BigDecimal returnQty) {
        this.returnQty = returnQty;
        return this;
    }

    public BigDecimal getCumulativeQty() {
        return cumulativeQty;
    }

    public ReturnRateData setCumulativeQty(BigDecimal cumulativeQty) {
        this.cumulativeQty = cumulativeQty;
        return this;
    }

    public BigDecimal getCumulativeTotalQty() {
        return cumulativeTotalQty;
    }

    public ReturnRateData setCumulativeTotalQty(BigDecimal cumulativeTotalQty) {
        this.cumulativeTotalQty = cumulativeTotalQty;
        return this;
    }

    public BigDecimal getCumulativeReturnQty() {
        return cumulativeReturnQty;
    }

    public ReturnRateData setCumulativeReturnQty(BigDecimal cumulativeReturnQty) {
        this.cumulativeReturnQty = cumulativeReturnQty;
        return this;
    }
}
